<?php
/**
 *   @description 首页
 *   @author Xiaohui Zeng
 *   @date 2019/12/1
 */
namespace app\api\controller;

use app\model\CmsContent;

/*
json_encode($jn_city); 数组转json字符串
json_decode($jn_city,false);第二个参数 true则返回array类型 false则返回object类型，可以默认不写
*/
class Index extends Base {

    public function index() {
        $dataArr = []; // 本页数据集合
        /**
         *   获取轮播图 取所有九级推荐九级头条的数据 最多4条 默认查不隐藏的
         *   @param
         *   @date 2019/12/2
         *   @author Xiaohui Zeng
         */
        $Redis = new Redis();
        $util = new util(); // 工具类
        $CmsLunboListData = $Redis->get('CmsLunboList-wx');
        if($CmsLunboListData) {
            $CmsLunboList = $CmsLunboListData;
        } else {
            $CmsLunboList = CmsContent::where(['hide'=>0,"isfirst"=>9,"isgood"=>9])->hidden(['content'])->order('newtime asc')->limit(5)->select();
//            $Redis->save('CmsLunboList-wx', $CmsLunboList,60*60*1); // 首页轮播缓存1小时
            for ($i = 0; $i < count($CmsLunboList); $i++) {
                $CmsLunboList[$i] = $util->tagName($CmsLunboList[$i]); // 处理tagname为数组
            }
        }
        $dataArr['CmsLunboList'] = $CmsLunboList;

        /**
         *   获取所有推荐的数据 最多8条 默认查不隐藏的
         *   @param
         *   @date 2019/12/2
         *   @author Xiaohui Zeng
         */
        $CmsIsgoodListData = $Redis->get('CmsIsgoodList-wx');
        if($CmsIsgoodListData) {
            $IsgoodArr = $CmsIsgoodListData;
        } else {
            $CmsIsgoodList = CmsContent::where(["hide"=>0,"isfirst"=>8,"isgood"=>8])->hidden(['content'])->order('newtime desc')->limit(8)->select();
            $IsgoodArr = [];
            for ($i=0;$i<count($CmsIsgoodList);$i++) {
                if($CmsIsgoodList[$i]['isfirst'] !== 9 && $CmsIsgoodList[$i]['isgood'] !== 9) {
                    $CmsIsgoodList[$i] = $util->tagName($CmsIsgoodList[$i]); // 处理tagname为数组
                    array_push($IsgoodArr,$CmsIsgoodList[$i]);
                }
            }
//            $Redis->save('CmsIsgoodList-wx', $IsgoodArr,60*60*1); // 首页推荐缓存1小时
        }
        $dataArr['CmsIsgoodList'] = $IsgoodArr;
        /**
         *   获取所有最新的数据 最多6条 默认查不隐藏的
         *   @param
         *   @date 2019/12/2
         *   @author Xiaohui Zeng
         */
        $CmsContent = new Content();
        $CmsZuixinList = $CmsContent ->index('', 1,6,'',0,'newtime');
        $dataArr['CmsZuixinList'] = $CmsZuixinList;
        /**
         *   获取作品 默认查不隐藏的
         *   @param
         *   @date 2019/12/2
         *   @author Xiaohui Zeng
         */
        $CmsZuopinList = $CmsContent ->index(25, 1,'','',0,'newtime');
        $dataArr['CmsZuopinList'] = $CmsZuopinList;

        /**
         *   获取全站tag
         *   @param
         *   @date 2019/12/2
         *   @author Xiaohui Zeng
         */
       /* $tagArr = new tag();
        $CmsTagList = $tagArr->index();
        $dataArr['CmsTagList'] = $CmsTagList;*/
        return $this->buildSuccess(['list' => $dataArr], '获取成功'); // 返回json 用于调试
    }
}
